package com.hctforgreen.greenservice.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.hctforgreen.greenservice.CaptureActivity;
import com.hctforgreen.greenservice.DebugActivity;
import com.hctforgreen.greenservice.DownloadManagerActivityV2;
import com.hctforgreen.greenservice.FeedbackActivity;
import com.hctforgreen.greenservice.MainActivityV2;
import com.hctforgreen.greenservice.MessageFirstActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.SettingActivity;
import com.hctforgreen.greenservice.StartPwdActivity;
import com.hctforgreen.greenservice.TechnicalDataActivityV4;
import com.hctforgreen.greenservice.VideoTypesActivity;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.db.DBMessageManager;
import com.hctforgreen.greenservice.model.BookTypeListEntity.BookTypeEntity;
import com.hctforgreen.greenservice.model.DataVersionCodeEntity;
import com.hctforgreen.greenservice.model.LoginResultEntity;
import com.hctforgreen.greenservice.model.MachineListEntity.MachineEntity;
import com.hctforgreen.greenservice.service.WifiUpdateService;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView;
import com.hctforgreen.greenservice.ui.widget.PullToRefreshListView.OnRefreshListener;
import com.hctforgreen.greenservice.utils.DataVersionCodeUtil;
import com.hctforgreen.greenservice.utils.DownloadBookLstInitFinishUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.LanguageSettingUtil;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;
import com.hctforgreen.greenservice.utils.MainFreshStoreUtil;
import com.hctforgreen.greenservice.utils.PersonPointsStoreUtil;
import com.hctforgreen.greenservice.utils.ScreenUtil;
import com.hctforgreen.greenservice.utils.Utils;
import com.umeng.analytics.MobclickAgent;

public class MainV2ListAdapter extends BaseAdapter implements OnRefreshListener {

	private Activity mActivity;
	private PullToRefreshListView mListView;
	private View mConvertView;
	private MainActivityV2 mSelf;

	public MainV2ListAdapter(Activity activity, PullToRefreshListView listView,
			MainActivityV2 self) {
		mActivity = activity;
		mListView = listView;
		mSelf = self;
		mListView.setAdapter(this);
		mListView.setOnRefreshListener(this);
	}

	@Override
	public int getCount() {
		return 1;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		convertView = View.inflate(mActivity, R.layout.layout_main_v2, null);
		mConvertView = convertView;
		dealTopBannerParams(convertView);
		renderPersionNameTv(convertView);
		renderPersionPointsTv();
		initWindow(convertView, false, null);
		return convertView;
	}

	private void initTitleButtonBar(View convertView) {
		/**
		 * 功能权限
		 */
		final String functions = LoginResultStoreUtil.get(mSelf).functions;

		/**
		 * 开机密码
		 */
		final Button mainTop1Btn = (Button) convertView
				.findViewById(R.id.btn_main_top_1);
		/**
		 * 条码扫描
		 */
		final Button mainTop2Btn = (Button) convertView
				.findViewById(R.id.btn_main_top_2);
		/**
		 * 调试维修
		 */
		final Button mainTop3Btn = (Button) convertView
				.findViewById(R.id.btn_main_top_3);
		/**
		 * 通知
		 */
		final Button mainCenter1Btn = (Button) convertView
				.findViewById(R.id.btn_main_center_1);
		/**
		 * 技术资料
		 */
		final Button mainCenter2Btn = (Button) convertView
				.findViewById(R.id.btn_main_center_2);
		/**
		 * 信息反馈
		 */
		final Button mainCenter3Btn = (Button) convertView
				.findViewById(R.id.btn_main_center_3);
		/**
		 * 下载管理
		 */
		final Button mainBottom1Btn = (Button) convertView
				.findViewById(R.id.btn_main_bottom_1);
		/**
		 * 软件设置
		 */
		final Button mainBottom2Btn = (Button) convertView
				.findViewById(R.id.btn_main_bottom_2);

		/**
		 * 视频播放
		 */
		final Button mainBottom3Btn = (Button) convertView
				.findViewById(R.id.btn_main_bottom_3);

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_STARTPWD)) {
			mainTop1Btn.setEnabled(false);
		} else {
			mainTop1Btn.setEnabled(true);
		}

		mainTop1Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (functions == null
				// || !functions
				// .contains(LoginResultEntity.FUNCTION_STARTPWD)) {
				// Toast.makeText(mSelf,
				// mSelf.getString(R.string.function_power),
				// Toast.LENGTH_LONG).show();
				// return;
				// }
				MobclickAgent.onEvent(mActivity, "Account_start_pwd");
				Intent intent = new Intent(mActivity, StartPwdActivity.class);
				// 选择是否需要定位并传参给下一个界面定位（默认是）
				intent.putExtra("loc", true);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mActivity.startActivity(intent);

			}
		});
		// // 开机密码 英文版或系统语言为非中文时显示为不可点
		// initStartPwdButton(mainTop1Btn);

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_CAPTURE)) {
			mainTop2Btn.setEnabled(false);
		} else {
			mainTop2Btn.setEnabled(true);
		}
		mainTop2Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_barcode_scan");
				Intent intent = new Intent(mActivity, CaptureActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mActivity.startActivityForResult(intent, mSelf.ZXING_DATA);
			}
		});

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_DEBUG)) {
			mainTop3Btn.setEnabled(false);
		} else {
			mainTop3Btn.setEnabled(true);
		}

		mainTop3Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_debug_service");

				Intent intent = new Intent(mActivity, DebugActivity.class);
				// 传递值（可能会用于区别直接进入调试还是通过扫码进入）
				intent.putExtra("params", "");
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				mActivity.startActivity(intent);
			}
		});

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_MESSAGEFIRST)) {
			mainCenter1Btn.setEnabled(false);
		} else {
			mainCenter1Btn.setEnabled(true);
		}

		mainCenter1Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_notification");
				Intent intent = new Intent();
				intent.setClass(mActivity, MessageFirstActivityV2.class);
				mActivity.startActivityForResult(intent,
						mSelf.REQUEST_UPDATE_MSG);
			}
		});
		if (functions == null
				|| !functions
						.contains(LoginResultEntity.FUNCTION_TECHNICALDATA)) {
			mainCenter2Btn.setEnabled(false);
		} else {
			mainCenter2Btn.setEnabled(true);
		}

		mainCenter2Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_technical_data");
				Intent intent = new Intent();
				intent.setClass(mActivity, TechnicalDataActivityV4.class);
				intent.putExtra(HctConstants.ON_ACTIVITY_KEY_MACHINE_ENTITY,
						new MachineEntity());
				mActivity.startActivity(intent);
			}
		});

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_FEEDBACK)) {
			mainCenter3Btn.setEnabled(false);
		} else {
			mainCenter3Btn.setEnabled(true);
		}
		mainCenter3Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_feadback");
				Intent intent = new Intent();
				intent.setClass(mActivity, FeedbackActivity.class);
				mActivity.startActivity(intent);
			}
		});

		if (functions == null
				|| !functions
						.contains(LoginResultEntity.FUNCTION_DOWNLOADMANAGER)) {
			mainBottom1Btn.setEnabled(false);
		} else {
			mainBottom1Btn.setEnabled(true);
		}
		mainBottom1Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_download_management");
				Intent intent = new Intent();
				intent.setClass(mActivity, DownloadManagerActivityV2.class);
				mActivity.startActivity(intent);
			}
		});

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_SETTING)) {
			mainBottom2Btn.setEnabled(false);
		} else {
			mainBottom2Btn.setEnabled(true);
		}
		mainBottom2Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				MobclickAgent.onEvent(mActivity, "Account_software_setting");
				Intent intent = new Intent();
				intent.setClass(mActivity, SettingActivity.class);
				mActivity.startActivity(intent);
			}
		});

		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_VIDEO)) {
			mainBottom3Btn.setEnabled(false);
		} else {
			mainBottom3Btn.setEnabled(true);
		}
		// mainBottom3Btn.setVisibility(View.INVISIBLE);
		mainBottom3Btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(mActivity, VideoTypesActivity.class);
				mActivity.startActivity(intent);
			}
		});
	}

	/**
	 * 多语言设置|系统语言设置 非中文版隐藏
	 */
	private void initStartPwdButton(final Button btn) {
		int languageType = LanguageSettingUtil.get(mActivity);
		if (languageType == 1) {// 英文版
			btn.setEnabled(false);
			return;
		}
		if (languageType == -1) {
			Locale locale = mActivity.getResources().getConfiguration().locale;
			String language = locale.getLanguage();
			if (!language.endsWith("zh")) {// 非中文版
				btn.setEnabled(false);
				return;
			}
		}

	}

	private void initWindow(View convertView, boolean bTopRefresh,
			DataVersionCodeEntity entity) {

		getAllSeries(convertView, bTopRefresh, entity);
		updateMsgCount(true, convertView);
		initTitleButtonBar(convertView);
	}

	public void updateMsgCount(final boolean update) {
		updateMsgCount(update, mConvertView);
	}

	/**
	 * 
	 * 更新消息未读数
	 * 
	 * @param update
	 *            是否需要从服务器同步
	 */
	private void updateMsgCount(final boolean update, View convertView) {
		/**
		 * 功能权限
		 */
		final String functions = LoginResultStoreUtil.get(mSelf).functions;
		if (functions == null
				|| !functions.contains(LoginResultEntity.FUNCTION_MESSAGEFIRST)) {
			return;
		}
		if (convertView == null) {
			return;
		}

		final TextView newMsgCountTv = (TextView) convertView
				.findViewById(R.id.tv_new_msg_count);

		final ImageView newMsgCountBgIv = (ImageView) convertView
				.findViewById(R.id.iv_new_msg_count);
		/**
		 * 未读通知条数获取
		 */
		new HandlerThread("get_count") {
			public void run() {
				try {
					if (update) {
						HctController controller = new HctController(mActivity);
						controller.getRecentMessages();
					}

					DBMessageManager dbMessageManager = new DBMessageManager(
							mActivity);
					final Integer count = dbMessageManager
							.getUnreadMessageCount();
					mActivity.runOnUiThread(new Runnable() {
						public void run() {

							if (count == null || count == 0) {
								newMsgCountTv.setVisibility(View.GONE);
								newMsgCountBgIv.setVisibility(View.GONE);
							} else {
								newMsgCountTv.setText(count + "");
								newMsgCountTv.setVisibility(View.VISIBLE);
								newMsgCountBgIv.setVisibility(View.VISIBLE);
							}

						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}
			};
		}.start();
	}

	private void renderPersionNameTv(View convertView) {
		if (convertView == null) {
			return;
		}
		final TextView persionNameTv = (TextView) convertView
				.findViewById(R.id.tv_persion_name);
		persionNameTv.setText(LoginResultStoreUtil.get(mActivity).personName);
	}

	/**
	 * 显示/刷新 积分信息
	 * 
	 * @param convertView
	 */
	public void renderPersionPointsTv() {
		if (mConvertView == null) {
			return;
		}

		final TextView pointsTv = (TextView) mConvertView
				.findViewById(R.id.tv_person_points);
		final TextView rankTv = (TextView) mConvertView
				.findViewById(R.id.tv_person_rank);
		final TextView ratingTv = (TextView) mConvertView
				.findViewById(R.id.tv_person_rating);
		pointsTv.setText(PersonPointsStoreUtil.get(mActivity).points);
		rankTv.setText(PersonPointsStoreUtil.get(mActivity).rank);
		ratingTv.setText(PersonPointsStoreUtil.get(mActivity).rating);
	}

	/**
	 * 1、所有系列信息
	 */
	private void getAllSeries(final View convertView,
			final boolean bTopRefresh, final DataVersionCodeEntity entity) {
		final LinearLayout loadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_load);
		final LinearLayout reloadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_reload);
		final Button reloadBtn = (Button) convertView
				.findViewById(R.id.btn_default_reload);

		final TextView loadTv = (TextView) convertView
				.findViewById(R.id.tv_default_load);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
					}

					getAllMachines(convertView, bTopRefresh, entity);

					break;

				case HctConstants.STATUS_FAILD:
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
						reloadBtn.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								getAllSeries(convertView, bTopRefresh, entity);
							}
						});
					}
					if (bTopRefresh) {
						mListView.onRefreshComplete();
					}

					break;
				}
			}

		};
		if (!bTopRefresh) {
			loadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			reloadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			loadTv.setVisibility(View.GONE);
			loadLyt.setVisibility(View.VISIBLE);
			reloadLyt.setVisibility(View.GONE);

		}

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller.getAllSeries(bTopRefresh);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 2、所有机型信息
	 */
	private void getAllMachines(final View convertView,
			final boolean bTopRefresh, final DataVersionCodeEntity entity) {
		final LinearLayout loadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_load);
		final LinearLayout reloadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_reload);
		final Button reloadBtn = (Button) convertView
				.findViewById(R.id.btn_default_reload);

		final TextView loadTv = (TextView) convertView
				.findViewById(R.id.tv_default_load);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
					}

					getAllColumns(convertView, bTopRefresh, entity);

					break;

				case HctConstants.STATUS_FAILD:
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
						reloadBtn.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								getAllMachines(convertView, bTopRefresh, entity);
							}
						});
					}
					if (bTopRefresh) {
						mListView.onRefreshComplete();
					}
					break;
				}
			}

		};
		if (!bTopRefresh) {
			loadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			reloadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			loadTv.setVisibility(View.GONE);
			loadLyt.setVisibility(View.VISIBLE);
			reloadLyt.setVisibility(View.GONE);

		}

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller.getAllMachines(bTopRefresh);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 调试维修条目
	 * 
	 * @param convertView
	 * @param bTopRefresh
	 */
	private void getAllColumns(final View convertView,
			final boolean bTopRefresh, final DataVersionCodeEntity entity) {
		final LinearLayout loadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_load);
		final LinearLayout reloadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_reload);
		final Button reloadBtn = (Button) convertView
				.findViewById(R.id.btn_default_reload);

		final TextView loadTv = (TextView) convertView
				.findViewById(R.id.tv_default_load);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					if (bTopRefresh) {
						DataVersionCodeUtil.save(mActivity, entity.versionCode);
						mListView.onRefreshComplete();
						return;
					}

					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);

					}

					if (DownloadBookLstInitFinishUtil
							.getInitFinished(mActivity)) {

						onRefresh();

						initTitleButtonBar(convertView);
						return;
					}

					getAllBooksWithAllChildSeriesAndDbSerieName(convertView,
							bTopRefresh, entity);

					break;

				case HctConstants.STATUS_FAILD:
					if (bTopRefresh) {
						mListView.onRefreshComplete();
						return;
					}
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
						reloadBtn.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								getAllColumns(convertView, bTopRefresh, entity);
							}
						});
					}

					break;
				}
			}

		};
		if (!bTopRefresh) {
			loadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			reloadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			loadTv.setVisibility(View.GONE);
			loadLyt.setVisibility(View.VISIBLE);
			reloadLyt.setVisibility(View.GONE);

		}

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller.getAllColumns(bTopRefresh);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 3、所有资料类别信息
	 */
	@Deprecated
	private void getAllBooktypes(final View convertView,
			final boolean bTopRefresh) {
		final LinearLayout loadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_load);
		final LinearLayout reloadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_reload);
		final Button reloadBtn = (Button) convertView
				.findViewById(R.id.btn_default_reload);

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.GONE);

					hctResult = (HctResult) msg.obj;

					List<BookTypeEntity> dataList = (List<BookTypeEntity>) hctResult.data;

					break;

				case HctConstants.STATUS_FAILD:
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.GONE);
					reloadBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							getAllBooktypes(convertView, bTopRefresh);
						}
					});
					break;
				}
			}

		};

		loadLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 用此法屏蔽掉FrameLayout下面Layout的点击事件
			}
		});
		reloadLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 用此法屏蔽掉FrameLayout下面Layout的点击事件
			}
		});
		loadLyt.setVisibility(View.VISIBLE);
		reloadLyt.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller.getAllBooktypes(bTopRefresh);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	private void getAllBooksWithAllChildSeriesAndDbSerieName(
			final View convertView, final boolean bTopRefresh,
			final DataVersionCodeEntity entity) {

		final LinearLayout loadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_load);
		final LinearLayout reloadLyt = (LinearLayout) convertView
				.findViewById(R.id.lyt_default_reload);
		final Button reloadBtn = (Button) convertView
				.findViewById(R.id.btn_default_reload);

		final TextView loadTv = (TextView) convertView
				.findViewById(R.id.tv_default_load);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:

					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);

					}

					initTitleButtonBar(convertView);

					break;

				case HctConstants.STATUS_FAILD:
					if (bTopRefresh) {
						mListView.onRefreshComplete();
						return;
					}
					if (!bTopRefresh) {
						loadLyt.setVisibility(View.GONE);
						reloadLyt.setVisibility(View.GONE);
						reloadBtn.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								getAllBooksWithAllChildSeriesAndDbSerieName(
										convertView, bTopRefresh, entity);
							}
						});
					}

					break;
				}
				Intent intent = new Intent(WifiUpdateService.MY_NAME);
				mActivity.startService(intent);
			}

		};
		if (!bTopRefresh) {
			loadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			reloadLyt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// 用此法屏蔽掉FrameLayout下面Layout的点击事件
				}
			});
			loadTv.setVisibility(View.GONE);
			loadLyt.setVisibility(View.VISIBLE);
			reloadLyt.setVisibility(View.GONE);

		}

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller
							.getAllBooksWithAllChildSeriesAndDbSerieName(bTopRefresh,0);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 按原图比例缩放顶部Banner
	 */
	private void dealTopBannerParams(View convertView) {
		ScreenUtil screenUtil = new ScreenUtil();
		int screenWidth = screenUtil.getScreenWidth(mActivity);

		int topBannerHeight = (screenWidth * 300 / 640);

		LinearLayout topBannerIv = (LinearLayout) convertView
				.findViewById(R.id.lyt_banner);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				topBannerHeight);
		topBannerIv.setLayoutParams(params);
	}

	private void getDataVersionCode() {

		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:
					// 当日首次刷新成功增加积分
					String shareDate = MainFreshStoreUtil.get(mActivity);
					final SimpleDateFormat dateFormat = new SimpleDateFormat(
							"yyyy-MM-dd");
					final String nowDate = dateFormat.format(new Date());
					if (!shareDate.equals(nowDate)) {// 当日首次刷新增加积分
						MainFreshStoreUtil.save(mActivity, nowDate);
						Utils.submitPoints(mActivity, 1, "", false);
					}

					hctResult = (HctResult) msg.obj;

					DataVersionCodeEntity entity = (DataVersionCodeEntity) hctResult.data;

					String localVersionCode = DataVersionCodeUtil
							.get(mActivity);

					if (!localVersionCode.equals(entity.versionCode)) {
						mListView.onRefreshShow();
						initWindow(mConvertView, true, entity);
						return;
					}

					mListView.onRefreshComplete();
					Intent intent = new Intent(WifiUpdateService.MY_NAME);
					mActivity.startService(intent);
					break;

				case HctConstants.STATUS_FAILD:
					mListView.onRefreshComplete();
					break;
				}
			}

		};

		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(mActivity);

					HctResult result = controller.getDataVersionCode();
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}

	/**
	 * 执行顶部刷新
	 */
	@Override
	public void onRefresh() {
		final View loadLyt = mActivity.findViewById(R.id.lyt_default_load);
		final View reloadLyt = mActivity.findViewById(R.id.lyt_default_reload);
		if (loadLyt == null || reloadLyt == null
				|| loadLyt.getVisibility() == View.VISIBLE
				|| reloadLyt.getVisibility() == View.VISIBLE) {
			mListView.onRefreshComplete();
			return;
		}

		getDataVersionCode();
	}

}
